package com.zm.demo.core.collection;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.System.out;

/**
 * @author zoum
 * @create 2019/7/29 16:15
 */
public class ListT {


    @Test
    public void test1(){
        int length = 100;
        List<Integer>list  = new ArrayList(length);
        for (int i = 0; i < length; i++) {
            if (i<10){
                list.add(i,2);
            }else if (i<20){
                list.add(i,3);
            }else if (i<30){
                list.add(i,4);
            }else if (i<45){
                list.add(i,5);
            }else if (i<65){
                list.add(i,6);
            }else if (i<80){
                list.add(i,7);
            }else if (i<90){
                list.add(i,8);
            }else if (i<95){
                list.add(i,9);
            }else if (i<100){
                list.add(i,10);
            }
        }

        Collections.shuffle(list);
        out.println(list.toString());
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            out.println(list.get(random.nextInt(length)));
        }


        out.println("count = " + list.stream().filter(v -> v > 1000).count());
        list.stream().filter(v -> v > 1000).limit(10).forEach(v -> out.println(v));
    }

    @Test
    public void testList(){
        out.println(Arrays.asList(1,2,3,4,5).stream().map(a->String.valueOf(a)).collect(Collectors.joining(",")));
    }
     @Test
    public void testListGson(){
        String s = "{a:b,b:[1,2,3]}";
         JsonObject jsonObject = new Gson().fromJson(s,JsonObject.class);
        out.println(jsonObject);
    }

    @Test
    public void testAdd(){
        long start = System.nanoTime();
        List<Integer> integers = new ArrayList<>(1);
        for (int i = 0; i < 10000 ; i++) {
            integers.add(i);
        }
        out.println("指定容量1耗时： " + (System.nanoTime() - start)/1000);

        start = System.nanoTime();
        List<Integer> integers1 = new ArrayList<>();
        for (int i = 0; i < 10000 ; i++) {
            integers1.add(i);
        }
        out.println("默认容量耗时： " + (System.nanoTime() - start)/1000);


        start = System.nanoTime();
        List<Integer> integers2 = new ArrayList<>(10000);
        for (int i = 0; i < 1000_0; i++) {
            integers2.add(i);
        }
        out.println("指定容量100耗时： " + (System.nanoTime() - start)/1000);
    }

    @Test
    public void listFilter(){
        List<Integer>list = new ArrayList<>();

        for (int i = 0; i < 100; i++) {
            list.add(i);
        }

        List list1 = list.stream().filter(i -> i%2 == 0).collect(Collectors.toList());
        out.println(list == list1);

    }


    public static void main(String[] args) {
        List<Integer>list = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            list.add(i);
        }

//        out.println(list);
        for (int j = 0; j < 100 ; j++) {
            Collections.shuffle(list);

//            long start = System.nanoTime();
//            for (int i = 0; i < 10000; i++) {
//                getMaxCnt(list);
//            }
//            out.println("遍历1: " + (System.nanoTime() - start) + "-" + getMaxCnt(list));

            long start1 = System.nanoTime();
            for (int i = 0; i < 10000; i++) {
                getMaxCntSort(list);
            }
            out.println("排序1: " + (System.nanoTime() - start1) + "-" + getMaxCntSort(list));
            out.println("-----------");
        }
    }

     private static Integer getMinCntSort(List<Integer> installCnts) {
        // 冒泡排序一趟选出最小期数
        for (int i = 0; i < installCnts.size() - 1; i++) {
            if (installCnts.get(i) < installCnts.get(i + 1)) {
                Integer temp = installCnts.get(i);
                installCnts.set(i, installCnts.get(i + 1));
                installCnts.set(i + 1, temp);
            }
        }
        return installCnts.get(installCnts.size() - 1);
    }

    /**
     * 获取最小期次
     *
     * @param installCnts
     * @return 期次列表中最小的期次
     */
    private static Integer getMinCnt(List<Integer> installCnts) {
        // 遍历一遍选出最小值
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < installCnts.size(); i++) {
            min = Math.min(min, installCnts.get(i));
        }
        return min;
    }

    /**
     * 获取最大期次
     *
     * @param installCnts 期次列表
     * @return 期次列表中最大的期次
     */
    private static Integer getMaxCnt(List<Integer> installCnts) {
        // 遍历一遍选出最大值
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < installCnts.size(); i++) {
            max = Math.max(max, installCnts.get(i));
        }
        return max;
    }

    private static Integer getMaxCntSort(List<Integer> installCnts) {
        // 冒泡排序一趟选出最大期数
        // TODO：这里不需要排序，遍历一遍就可以
        for (int i = 0; i < installCnts.size() - 1; i++) {
            if (installCnts.get(i) > installCnts.get(i + 1)) {
                Integer temp = installCnts.get(i);
                installCnts.set(i, installCnts.get(i + 1));
                installCnts.set(i + 1, temp);
            }
        }
        return installCnts.get(installCnts.size() - 1);
    }



    @Test
    public void testDistinct(){
        String k = "1,2,3,1,1,1,1,1";
        out.println(Stream.of(k.split(",")).distinct().collect(Collectors.toList()));
    }

    @Test
    public void testDistinct1(){
        String specialDelayCap = "AA";
        out.println(Arrays.stream(specialDelayCap.split(",")).anyMatch(item -> Objects.equals("A", item)));
    }

    @Test
    public void testAddAll(){
        List<Integer> list = null;
        List<Integer> list1 = new ArrayList<>();
        out.println(list1.addAll(list));
    }

    @Test
    public void testStream(){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

        Map<Integer, Long> m =  list.stream().collect(Collectors.groupingBy(s -> s, Collectors.counting()));
        out.println(m);
        out.println(m.get(6));

    }

}
